const app = getApp()
import {
  reqConsumerRoomList,
  reqConsumerDeviceUpdate,
  reqConsumerRoomAdd
} from '../../../../request/api.js';
import {
  showMsg
} from '../../../../utils/config.js'

Page({
    data: {
      dataList: [{
        id: 0,
        name: '我的家',
      }, {
        id: 2,
        name: '深圳家',
      }, {
        id: 3,
        name: '广州家',
      }, {
        id: 4,
        name: '珠海家',
      }, {
        id: 5,
        name: '佛山家',
      }, ],
      gonzhonghao1: '/statics/images/gaojing/yangan.png',
      list:['书房','书房1','书房2','书房3','书房4','书房5','书房6书房9书房9','书房7','书房8','书房9书房9书房9书房9书房9','书房9书房9书房9书房9书房9','书房9书房9书房9书房9书房9','书房9书房9书房9书房9书房9',],
      roomId:'',
      showInputDialog: false,
      deviceCode:"",
      deviceName:"",
      pic:""
    },
  
    onLoad(options) {
      console.log(options,'options');
      this.setData({
        deviceCode:options.deviceCode,
        pic:options.pic,
        deviceName:options.deviceName ||'烟感探测报警器'
      })
    },
   
    async getDataList() {
      let params = {
        homeId: app.globalData.familyInfo.id,
      }
      const res = await reqConsumerRoomList(params)
      if (res.code !== 20000) return showMsg(res.message)
      let {
        rows
      } = res.data
      this.setData({
        dataList: rows || []
      })
      this.setData({
        roomId:this.data.dataList[0].id
      })
    },
  
    onShow() {
      this.getDataList()
    },
    
    goname(item){
        // console.log(item.currentTarget.dataset.item);
        this.setData({
          roomId:item.currentTarget.dataset.item.id
        })
    },
    onAddRoom(){
      this.setData({
        showInputDialog: true
      })
      // wx.navigateTo({
      //   url: '/packageC/pages/room/add/add',
      // })
    },
 async   inputDialogHandle(e) {
      console.log(e);
      const {
        actionType,
        value
      } = e.detail
      this.setData({
        showInputDialog: false
      })
      if (actionType !== 'confirm') return
      const res = await reqConsumerRoomAdd({
        roomName:value,
        homeId: app.globalData.familyInfo.id,
      })
      if (res.code != 20000) return showMsg(res.message);
      this.setData({
        showInputDialog: false
      })
      this.getDataList()
    },
  
  async  goAdd(){
      const res = await reqConsumerDeviceUpdate({
        deviceCode:this.data.deviceCode,
        deviceName:this.data.deviceName,
        roomId:this.data.roomId
      })
      console.log(res, 'res');
      if (res.code !== 20000) return showMsg(res.message)
      wx.navigateTo({
        url: `/packageC/pages/device/nameSet/nameSet?roomId=${this.data.roomId}&deviceCode=${this.data.deviceCode}&pic=${this.data.pic}&deviceName=${this.data.deviceName}`,
      })
    },
  })